package com.dailyblue.java.project.smbms.shopping.service.impl;

import com.dailyblue.java.commons.util.JsonResult;
import com.dailyblue.java.commons.util.ResultTool;
import com.dailyblue.java.project.smbms.shopping.bean.Shopping;
import com.dailyblue.java.project.smbms.shopping.feign.GoodsFeign;
import com.dailyblue.java.project.smbms.shopping.feign.UserFeign;
import com.dailyblue.java.project.smbms.shopping.service.ShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Slf4j
public class ShoppingServiceImpl implements ShoppingService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private GoodsFeign goodsFeign;
    @Resource
    private UserFeign userFeign;

    @Override
    public JsonResult addCart(String token, int goodsId, int num) {
        String userId = getUserIdByToken(token);
        log.info("开始加入购物车，商品是：{},数量:{},用户token:{},用户ID:{}", goodsId, num, token,userId);
        HashOperations hash = stringRedisTemplate.opsForHash();
        // 判断这个用户token是否存在购物车
        Object v = hash.get("cart:" + userId, goodsId + "");
        if (v != null) {
            num += Integer.parseInt(v.toString());
        }
        hash.put("cart:" + userId, goodsId + "", num + "");
        return ResultTool.success();
    }

    @Override
    public JsonResult showCart(String token) {
        HashOperations hash = stringRedisTemplate.opsForHash();
        String userId = getUserIdByToken(token);
        // 获取到指定用户的所有购物车信息
        Map<String, String> map = hash.entries("cart:" + userId);
        Set<String> set = map.keySet();
        List<Shopping> list = new ArrayList<>();
        for (String s : set) {
            int goodsId = Integer.parseInt(s);
            int num = Integer.parseInt(map.get(s));
            JsonResult goods = goodsFeign.find(goodsId);
            Shopping shopping = new Shopping();
            shopping.setNum(num);
            shopping.setGoods(goods);
            list.add(shopping);
        }
        return ResultTool.success(list);
    }

    @Override
    public JsonResult removeGoods(String token, int goodsId) {
        HashOperations hash = stringRedisTemplate.opsForHash();
        hash.delete("cart:" + getUserIdByToken(token), goodsId + "");
        return ResultTool.success();
    }

    @Override
    public JsonResult updateCart(String token, int goodsId, int num) {
        HashOperations hash = stringRedisTemplate.opsForHash();
        String userId = getUserIdByToken(token);
        // 判断这个用户token是否存在购物车
        Object v = hash.get("cart:" + userId, goodsId + "");
        hash.put("cart:" + userId, goodsId + "", num + "");
        return null;
    }

    private String getUserIdByToken(String token){
        JsonResult result = userFeign.findUserIdByToken(token);
        if(!result.getSuccess()){
            return null;
        }
        return result.getData().toString();
    }
}
